using System.Net.Http;

namespace ChatWithMultAI.ViewModels.Agent;

public static class AgentHost
{
    private static readonly object _gate = new();
    private static IChatAgent? _agent;

    public static IChatAgent? Instance
    {
        get { lock (_gate) return _agent; }
    }

    public static async Task EnsureStartedAsync(string ownerUserId, string baseAddress)
    {
        if (_agent is not null) return;
        lock (_gate)
        {
            if (_agent is null)
            {
                var http = new HttpClient { BaseAddress = new Uri(baseAddress, UriKind.RelativeOrAbsolute) };
                _agent = new ChatAgent(http, ownerUserId);
            }
        }
        if (_agent is ChatAgent ca)
            await ca.InitializeAsync();
    }
}
